package com.tushare.controller;

import com.tushare.entity.*;
import com.tushare.network.WapResponse;
import com.tushare.util.ServiceUtil;
import com.tushare.util.TransformClassUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@CrossOrigin("*")
public class FinanceController {
    private static final String TAG = "FinanceController";
    private final TransformClassUtil transformClassUtil = new TransformClassUtil();
    @Autowired
    RestTemplate restTemplate;

    /**
     * 利润表
     *
     * @param ts_code 股票代码
     * @return
     */
    @GetMapping(value = "income", params = {"ts_code"})
    public List<Income> getIncome(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("income");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Income.class);
    }

    /**
     * 资产负债表
     *
     * @param ts_code 股票代码
     */
    @GetMapping(value = "balanceSheet", params = {"ts_code"})
    public List<BalanceSheet> getBalanceSheet(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("balancesheet");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, BalanceSheet.class);
    }

    /**
     * 现金流量表
     *
     * @param ts_code 股票代码
     */
    @GetMapping(value = "cashFlow", params = {"ts_code"})
    public List<Cashflow> getCashFlow(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("cashflow");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Cashflow.class);
    }

    /**
     * 业绩预告
     *
     * @param ts_code 股票代码
     */
    @GetMapping(value = "forecast", params = {"ts_code"})
    public List<Forecast> getForecast(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("forecast");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Forecast.class);
    }

    /**
     * 业绩快报
     *
     * @param ts_code 股票代码
     */
    @GetMapping(value = "express", params = {"ts_code"})
    public List<Express> getExpress(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("express");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Express.class);
    }

    /**
     * 分红送股
     */
    @GetMapping("dividend")
    public List<Dividend> getDividend(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("dividend");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Dividend.class);
    }

    /**
     * 财务指标数据
     *
     * @param ts_code 股票代码
     */
    @GetMapping(value = "fina_indicator", params = {"ts_code"})
    public List<FinaIndicator> getFinaIndicator(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("fina_indicator");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, FinaIndicator.class);
    }

}
